package org.example.menu;

import java.util.ArrayList;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;

public class Test {
    //    8.计算菜单中的总卡路里
    public static void main(String[] args) {
        Menu m1 = new Menu("yy", 111);
        Menu m2 = new Menu("uu", 222);
        Menu m3 = new Menu("aa", 333);
        Menu m4 = new Menu("ww", 500);
        Menu m5 = new Menu("rr", 800);
        Menu m6 = new Menu("bb", 900);
        List<Menu> listMenu = new ArrayList<>();
        listMenu.add(m1);
        listMenu.add(m2);
        listMenu.add(m3);
        listMenu.add(m4);
        listMenu.add(m5);
        listMenu.add(m6);

        DoubleSummaryStatistics st = listMenu.stream()
                .mapToDouble((m) -> m.getCalorie()).summaryStatistics();
        System.out.println("总卡路里" + st.getSum());


        //  9.统计菜单中菜的个数
        long count = listMenu.stream().count();
        System.out.println("菜单中菜的个数" + count);

        //18.数值流计算菜单的总卡路里
        double sum = listMenu.stream().
                mapToDouble(Menu::getCalorie).sum();
        System.out.println(sum);

//        22.求出菜单列表的总热量，平均值等

        DoubleSummaryStatistics summaryStatistics = listMenu.stream().mapToDouble((menu) -> menu.getCalorie()).summaryStatistics();
        System.out.println("菜单列表的总热量" + summaryStatistics.getSum() + "卡路里");
        System.out.println("菜单列表的平均热量" + summaryStatistics.getAverage() + "卡路里");

//        23.得到一个逗号分隔的菜肴名称列表
        String names = listMenu.stream().map(menu -> menu.getName()).collect(Collectors.joining(","));
        System.out.println(names);

//        24.用reducing方法计算菜单的总热量
        double sumHot = listMenu.stream().map(Menu::getCalorie)
                .reduce(0d, (a, b) -> a + b);


        double sumHot2 = listMenu.stream().collect(Collectors.reducing(0d,
                Menu::getCalorie, (a, b) -> a + b));

        System.out.println(sumHot);

        System.out.println("-----");
        System.out.println(sumHot2);
        //25.用reduce方法实现 collector的toList方法
        List<Double> coll =listMenu.stream().map(o->o.getCalorie()).distinct().collect(Collectors.toList());
        List<Double> list =coll.stream().reduce(new ArrayList(),(List l ,Double d)->{
            l.add(d);
            return l;
        },(List l1,List l2)->{
            l1.addAll(l2);
            return l1;
        });
        System.out.println(list);


     /*   26.把热量不到400卡路里的菜划分为“低热量”(diet),热量400到700卡路里的菜划为“普通”(normal),高于700卡路里的划为“高热量”(fat)
*/
        Map<CalorieLevel,List<Menu>> collect=listMenu.stream()
                .collect(groupingBy(a->{
                    double calorice=a.getCalorie();
                    if(calorice<400){
                        return CalorieLevel.DIET;
                    }else if(calorice<700){
                        return CalorieLevel.NORMAL;
                    }else {
                        return CalorieLevel.FAT;
                    }
                }));
        collect.keySet()
                .forEach(calorieLevel -> System.out.println(calorieLevel+"="+collect.get(calorieLevel)));
    }


}
